package com.zryc.shop.pms.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.pms.entity.ProductCategory;
import com.zryc.shop.pms.service.IProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@RestController
@Api(tags = "商品类别管理接口列表")
@RequestMapping("/pms/productCategory")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService productCategoryService;

    @GetMapping("/list/{parentId}")
    @ApiOperation("分页查询全部商品分类列表")
    public Result list(@PathVariable @ApiParam(value = "父id") Integer parentId,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页大小", required = true) Integer pageSize) {
        return Result.ok(productCategoryService.list(parentId, pageNum, pageSize));
    }

    @GetMapping("/list/withChildren")
    @ApiOperation("分页查询商品分类列表")
    public Result withChildren(){
        List<ProductCategory> productCategories = productCategoryService.list();
        return Result.ok(productCategoryService.buildTree(productCategories));
    }

    @PostMapping("/update/navStatus")
    @ApiOperation("修改是否显示在导航栏")
    public Result updateNavStatus(@RequestParam @ApiParam(value = "类别ID", required = true) String ids,
                                  @RequestParam @ApiParam(value = "导航栏显示状态", required = true, allowableValues = "0,1") Integer navStatus){
        return Result.ok(productCategoryService.updateNavStatus(ids,navStatus));
    }

    @PostMapping("/update/showStatus")
    @ApiOperation("修改是否显示")
    public Result updateShowStatus(@RequestParam @ApiParam(value = "类别ID", required = true) String ids,
                                   @RequestParam @ApiParam(value = "显示状态", required = true, allowableValues = "0,1") Integer showStatus){
        return Result.ok(productCategoryService.updateShowStatus(ids,showStatus));
    }

    @PostMapping("/create")
    @ApiOperation("创建商品类别")
    public Result create(@RequestBody ProductCategory category){
        return Result.ok(productCategoryService.save(category));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除商品类别")
    public Result delete(@PathVariable @ApiParam(value = "类别ID", required = true) Long id){
        return Result.ok(productCategoryService.removeCategory(id));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询商品类别详情")
    public Result detail(@PathVariable @ApiParam(value = "类别ID", required = true) Long id){
        return Result.ok(productCategoryService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改商品类别")
    public Result update(@PathVariable @ApiParam(value = "类别ID", required = true) Long id, @RequestBody ProductCategory category){
        return Result.ok(productCategoryService.updateCategory(id,category));
    }



}
